/*********************************************************************
*                SEGGER Microcontroller GmbH & Co. KG                *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2010  SEGGER Microcontroller GmbH & Co. KG       *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V5.06 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only be used in accordance with a license and should not be re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GUICore.C
Purpose     : Core routines
---------------------------END-OF-HEADER------------------------------
*/

#include <stddef.h>           /* needed for definition of NULL */

#define  GL_CORE_C

#include "GUI_Private.h"

#if GUI_WINSUPPORT
  #include "WM_GUI.h"
  #include "WM.h"
#endif

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
static const tLCD_HL_APIList _HL_APIList = {
  LCD_DrawHLine,
  LCD_DrawPixel
};

const GUI_FONT GUI_UNI_PTR * GUI__pFontDefault = GUI_DEFAULT_FONT;

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _InitContext
*
* Purpose
*   Initializes the given context.
*
* Special considerations
*   Make sure that no GUI functions are called, because locking is
*   not allowed here (GUITASK_INIT not yet called).
*/
static void _InitContext(GUI_CONTEXT * pContext) {
  //
  // Check if a display driver has been added
  //
  while (GUI_Context.apDevice[0] == NULL); // Stop if no display driver has been added
  //
  // Check if a default font has been specified
  //
  while (GUI__pFontDefault == NULL);       // Stop if no default font has been defined
  //
  // Initialize members of pContext. 
  // memset(..,0,..) is not required, as this function is called only at startup of the GUI when data is 0
  //
  pContext->pClipRect_HL = &GUI_Context.ClipRect;
  GUI_Context.apDevice[GUI_Context.SelLayer]->pDeviceAPI->pfGetRect(GUI_Context.apDevice[GUI_Context.SelLayer], &pContext->ClipRect);
  pContext->pLCD_HL      = &_HL_APIList;
  pContext->pAFont       = GUI__pFontDefault;
  pContext->pClipRect_HL = &GUI_Context.ClipRect;
  pContext->PenSize      = 1;
  //
  // Variables in WM module
  //
  #if GUI_WINSUPPORT
    pContext->hAWin    = WM_GetDesktopWindow();
  #endif
  //
  // Variables in GUI_AA module
  //
  pContext->AA_Factor = 3;
  pContext->Color   = GUI_INVALID_COLOR;
  pContext->BkColor = GUI_INVALID_COLOR;
  LCD_SetBkColor(GUI_DEFAULT_BKCOLOR);
  LCD_SetColor(GUI_DEFAULT_COLOR);
  GUI_pUC_API = &GUI_UC_None;
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       GUI_SetDefaultFont
*/
void GUI_SetDefaultFont(const GUI_FONT GUI_UNI_PTR * pFont) {
  GUI__pFontDefault = pFont;
}

/*********************************************************************
*
*       GUI_ClearRect
*/
void GUI_ClearRect(int x0, int y0, int x1, int y1) {
  GUI_DRAWMODE PrevDraw;
  #if GUI_WINSUPPORT
    GUI_RECT r;
  #endif
  GUI_LOCK();
  PrevDraw = LCD_SetDrawMode(GUI_DRAWMODE_REV);
  #if GUI_WINSUPPORT
    WM_ADDORG(x0,y0);
    WM_ADDORG(x1,y1);
    r.x0 = x0;
    r.x1 = x1;
    r.y0 = y0;
    r.y1 = y1;
    WM_ITERATE_START(&r) {
  #endif
  LCD_FillRect(x0,y0,x1,y1);
  #if GUI_WINSUPPORT
    } WM_ITERATE_END();
  #endif
  LCD_SetDrawMode(PrevDraw);
  GUI_UNLOCK();
}

/*********************************************************************
*
*       GUI_Clear
*/
void GUI_Clear(void) {
  GUI_GotoXY(0,0);     /* Reset text cursor to upper left */
  GUI_ClearRect(GUI_XMIN, GUI_YMIN, GUI_XMAX, GUI_YMAX);
}

/*********************************************************************
*
*       GUI__Config
*/
void GUI__Config(void) {
  static int Done;
  if (Done == 0) {
    Done = 1;
    GUI_X_Config();
    LCD_X_Config();
  }
}

/*********************************************************************
*
*       GUI_Init
*
* Purpose:
*   Init of GUI internal data structures & variables
*/
int GUI_Init(void) {
  int r;
  GUI_DEBUG_LOG("\nGUI_Init()");
  GUI__Config();
  /* Init system wide globals first */
  GUI_DecChar = '.';
  GUI_X_Init();
  /* Init context */
  _InitContext(&GUI_Context);
  GUITASK_INIT();
  r = LCD_Init();
  GUI_SetOrg(0, 0);
  #if GUI_WINSUPPORT
    WM_Init();
  #endif
  GUITASK_COPY_CONTEXT();
  #if defined(GUI_TRIAL_VERSION)
    #if GUI_TRIAL_VERSION
      GUI_GotoXY(0, 0);
      GUI_DispString(  "This software\n"
	                     "contains an eval-\n"
	                     "build of emWin.\n"
	                     "\n"
	                     "A license is\n"
	                     "required to use\n"
	                     "it in a product.\n\n"
	                     "www.segger.com\n");
      GUI_GotoXY(0, 0);
      GUI_X_Delay(1000);
      GUI_Clear();
    #endif
  #endif
  return r;
}

/*************************** End of file ****************************/
